#ifndef PLATFORMS_H
#define PLATFORMS_H
inline QList<QPair<QString, char>> getRemovableDrives()
{
	QList<QPair<QString, char>> drives;
#ifdef _WIN32
	QFileInfoList list = QDir::drives();
	for (const QFileInfo& drive : list) {
		QString path = drive.filePath();
		char driveLetter = path.at(0).toLatin1();
		UINT type = GetDriveType((WCHAR*) path.utf16());
		if (type == DRIVE_REMOVABLE) {
			wchar_t volumeName[MAX_PATH];
			if (GetVolumeInformation((WCHAR*) path.utf16(), volumeName, MAX_PATH, NULL, NULL, NULL, NULL, 0)) {
				QString label = QString::fromWCharArray(volumeName);
				if (label.isEmpty()) label = "U盘";
				drives.append(qMakePair(QString("%1 (%2:)").arg(label).arg(driveLetter), driveLetter));
			} else {
				drives.append(qMakePair(QString("%1:").arg(driveLetter), driveLetter));
			}
		}
	}
#elif __linux__
#elif __APPLE__
#endif
	return drives;
}
inline bool ejectRemovableDriveInternal(char driveLetter)
{
#ifdef _WIN32
	QString path = QString(R"(\\.\%1:)").arg(driveLetter);
	WCHAR wPath[256] = {0};
	path.toWCharArray(wPath);
	HANDLE hDevice = CreateFile(wPath, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
	if (hDevice == INVALID_HANDLE_VALUE) { return false; }
	DWORD bytesReturned;
	if (!DeviceIoControl(hDevice, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0, &bytesReturned, NULL)) {
		CloseHandle(hDevice);
		return false;
	}
	if (!DeviceIoControl(hDevice, FSCTL_DISMOUNT_VOLUME, NULL, 0, NULL, 0, &bytesReturned, NULL)) {
		CloseHandle(hDevice);
		return false;
	}
	PREVENT_MEDIA_REMOVAL pmr = {0};
	pmr.PreventMediaRemoval = FALSE;
	if (!DeviceIoControl(hDevice, IOCTL_STORAGE_MEDIA_REMOVAL, &pmr, sizeof(pmr), NULL, 0, &bytesReturned, NULL)) {
		CloseHandle(hDevice);
		return false;
	}
	if (!DeviceIoControl(hDevice, IOCTL_STORAGE_EJECT_MEDIA, NULL, 0, NULL, 0, &bytesReturned, NULL)) {
		CloseHandle(hDevice);
		return false;
	}
	CloseHandle(hDevice);
	return true;
#elif __linux__
#elif __APPLE__
#endif
	return false;
}
inline bool forceEjectRemovableDrive(char driveLetter)
{
#ifdef _WIN32
	QString path = QString(R"(\\.\%1:)").arg(driveLetter);
	WCHAR wPath[256] = {0};
	path.toWCharArray(wPath);
	HANDLE hDevice = CreateFile(wPath, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
	if (hDevice == INVALID_HANDLE_VALUE) return (qDebug() << "Failed to open device:" << GetLastError()), false;
	DWORD bytesReturned;
	PREVENT_MEDIA_REMOVAL pmr = {0};
	pmr.PreventMediaRemoval = FALSE;
	BOOL result = DeviceIoControl(hDevice, IOCTL_STORAGE_EJECT_MEDIA, &pmr, sizeof(pmr), NULL, 0, &bytesReturned, NULL);
	CloseHandle(hDevice);
	return result;
#elif __linux__
#elif __APPLE__
#endif
	return false;
}
inline bool isDesktopActive()
{
#ifdef _WIN32
	if (HWND h = GetForegroundWindow()) {
		wchar_t c[256];
		GetClassName(h, c, 255);
		return !wcscmp(c, L"Progman") || !wcscmp(c, L"SHELLDLL_DefView") || !wcscmp(c, L"Shell_TrayWnd") || (GetWindowLong(h, GWL_EXSTYLE) & WS_EX_TOOLWINDOW) || IsIconic(h);
	}
	return true;
#elif __linux__
#elif __APPLE__
#endif
	return false;
}
inline bool isAudioPlaying()
{
#ifdef _WIN32
	CoInitialize(NULL);
	IMMDeviceEnumerator* pEnumerator = NULL;
	IMMDevice* pDevice = NULL;
	IAudioSessionManager2* pSessionManager = NULL;
	IAudioSessionEnumerator* pSessionList = NULL;
	bool audioPlaying = false;
	if (SUCCEEDED(CoCreateInstance(__uuidof(MMDeviceEnumerator), NULL, CLSCTX_ALL, __uuidof(IMMDeviceEnumerator), (void**) &pEnumerator)) && SUCCEEDED(pEnumerator->GetDefaultAudioEndpoint(eRender, eConsole, &pDevice)) && SUCCEEDED(pDevice->Activate(__uuidof(IAudioSessionManager2), CLSCTX_ALL, NULL, (void**) &pSessionManager)) && SUCCEEDED(pSessionManager->GetSessionEnumerator(&pSessionList))) {
		int sessionCount = 0;
		pSessionList->GetCount(&sessionCount);
		for (int i = 0; i < sessionCount; ++i) {
			IAudioSessionControl* pSession = NULL;
			IAudioSessionControl2* pSession2 = NULL;
			if (SUCCEEDED(pSessionList->GetSession(i, &pSession)) && SUCCEEDED(pSession->QueryInterface(__uuidof(IAudioSessionControl2), (void**) &pSession2))) {
				DWORD pid = 0;
				if (SUCCEEDED(pSession2->GetProcessId(&pid)) && pid != 0) {
					AudioSessionState state;
					if (SUCCEEDED(pSession->GetState(&state)) && state == AudioSessionStateActive) {
						audioPlaying = true;
						pSession->Release();
						pSession2->Release();
						break;
					}
				}
				pSession2->Release();
			}
			if (pSession) pSession->Release();
		}
	}
	if (pSessionList) pSessionList->Release();
	if (pSessionManager) pSessionManager->Release();
	if (pDevice) pDevice->Release();
	if (pEnumerator) pEnumerator->Release();
	CoUninitialize();
	return audioPlaying;
#elif __linux__
#elif __APPLE__
#endif
	return false;
};
#endif
